package icasue.base.config;

import icasue.base.config.filter.ElCondition;
import icasue.base.config.properties.CurrentLockProperties;
import icasue.base.register.ICasuePluginsBeanDefinitionRegistryPostProcessor;
import icasue.base.lock.DistributedLock;
import icasue.base.lock.redis.DataRedisDistributedLock;
import icasue.base.lock.redis.JedisDistributedLock;
import icasue.base.touch.ApplicationTouch;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Lazy;

/**
 * @Author: Qiao Hang
 * @CreateDate: 2021/11/26 下午2:31
 * @UpdateDate:
 * @Description:
 */
public class PluginBaseConfiguration {

    @Bean
    @Lazy(value = false)
    @ConditionalOnMissingBean(value = ApplicationTouch.class)
    public ApplicationTouch applicationTouch(){
        return new ApplicationTouch();
    }

    @Bean("InternalICasuePluginsBeanDefinitionRegistryPostProcessor")
    public ICasuePluginsBeanDefinitionRegistryPostProcessor iCasuePluginsBeanDefinitionRegistryPostProcessor(){
        return new ICasuePluginsBeanDefinitionRegistryPostProcessor();
    }

    @Bean
    @ConditionalOnMissingBean(DistributedLock.class)
    @ElCondition({
            "#environment.getProperty('concurrency.lock.enable','false').equals('true')",
            "#environment.getProperty('concurrency.lock.reference.jedisPool') != null",
            "#beanFactory.containsBean(#environment.getProperty('concurrency.lock.reference.jedisPool'))"
    })
    public JedisDistributedLock jedisDistributedLock(@Autowired ICasuePluginsBeanDefinitionRegistryPostProcessor registerProcessor){
        CurrentLockProperties currentLockProperties = registerProcessor.readConfigProperties(CurrentLockProperties.class);
        Object jedisPool = registerProcessor.beanFactory.getBean(currentLockProperties.reference.jedisPool);
        return new JedisDistributedLock(jedisPool, currentLockProperties.keyPrefix, currentLockProperties.timeoutMillions);
    }


    @Bean
    @ConditionalOnMissingBean(DistributedLock.class)
    @ElCondition({
            "#environment.getProperty('concurrency.lock.enable','false').equals('true')",
            "#environment.getProperty('concurrency.lock.reference.redisConnectionFactory') != null",
            "#beanFactory.containsBean(#environment.getProperty('concurrency.lock.reference.redisConnectionFactory'))"
    })
    public DataRedisDistributedLock dataRedisDistributedLock(@Autowired ICasuePluginsBeanDefinitionRegistryPostProcessor registerProcessor){
        CurrentLockProperties currentLockProperties = registerProcessor.readConfigProperties(CurrentLockProperties.class);
        Object redisConnectionFactory = registerProcessor.beanFactory.getBean(currentLockProperties.reference.redisConnectionFactory);
        return new DataRedisDistributedLock(redisConnectionFactory, currentLockProperties.keyPrefix, currentLockProperties.timeoutMillions);
    }
}
